package com.anan.builder;

import com.anan.bean.TableInfo;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class BuildException {

    public static void create(String projectName){

        createBusinessException(projectName);
        createAGlobalExceptionHandlerController(projectName);


    }
    private static void createAGlobalExceptionHandlerController(String projectName) {
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/java/com/anan/exception";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, "AGlobalExceptionHandlerController.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.exception;\n" +
                    "\n" +
                    "\n" +
                    "import com.anan.utils.Result;\n" +
                    "import org.slf4j.Logger;\n" +
                    "import org.slf4j.LoggerFactory;\n" +
                    "\n" +
                    "import org.springframework.web.bind.annotation.ExceptionHandler;\n" +
                    "import org.springframework.web.bind.annotation.RestController;\n" +
                    "import org.springframework.web.bind.annotation.RestControllerAdvice;\n" +
                    "import javax.servlet.http.HttpServletRequest;\n" +
                    "\n" +
                    "\n" +
                    "// 全局异常信息处理\n" +
                    "@RestControllerAdvice  // 是Spring中的一个注解，用于全局处理REST请求，包括异常处理、数据绑定、数据验证等。它可以应用到整个应用程序，也可以应用于某个特定的包或类。\n" +
                    "@RestController\n" +
                    "public class AGlobalExceptionHandlerController {\n" +
                    "\n" +
                    "    private static final Logger logger = LoggerFactory.getLogger(AGlobalExceptionHandlerController.class);\n" +
                    "\n" +
                    "    // @ExceptionHandler用于处理Controller中抛出的异常。当Controller中抛出指定的异常类型时，Spring会自动调用使用了@ExceptionHandler注解的方法来处理该异常。这样可以统一处理异常，并返回统一的错误信息给客户端。\n" +
                    "    @ExceptionHandler(value = Exception.class)\n" +
                    "    // 拦截全局错误信息 将方法的返回值作为相应数据返回\n" +
                    "    Result handleException(Exception e, HttpServletRequest request) {\n" +
                    "        logger.error(\"请求错误，请求地址:\"+request.getRequestURL()+\",错误信息:\"+ e.getMessage());\n" +
                    "        Result ajaxResponse = new Result();\n" +
                    "        //404\n" +
                    "        if (e instanceof BusinessException) {\n" +
                    "            ajaxResponse.setCode(400);\n" +
                    "            ajaxResponse.setMsg(e.getMessage());\n" +
                    "        }else {\n" +
                    "            ajaxResponse.setMsg(e.getMessage());\n" +
                    "            ajaxResponse.setCode(500);\n" +
                    "        }\n" +
                    "        return ajaxResponse;\n" +
                    "    }\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private static void createBusinessException(String projectName) {
        String path = System.getProperty("user.dir") + "/" + projectName + "/src/main/java/com/anan/exception";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        File beanFile = new File(path, "BusinessException.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.exception;\n" +
                    "\n" +
                    "\n" +
                    "public class BusinessException extends RuntimeException {\n" +
                    "\n" +
                    "    private static final long serialVersionUID = 6397537042184993222L;\n" +
                    "\n" +
                    "    private Integer code;\n" +
                    "\n" +
                    "    private String message;\n" +
                    "\n" +
                    "    public BusinessException(Integer code, String message) {\n" +
                    "        super(message);\n" +
                    "        this.code = code;\n" +
                    "        this.message = message;\n" +
                    "    }\n" +
                    "    public BusinessException(String message, Throwable e) {\n" +
                    "        super(message, e);\n" +
                    "        this.message = message;\n" +
                    "    }\n" +
                    "\n" +
                    "    public BusinessException(String message) {\n" +
                    "        super(message);\n" +
                    "        this.message = message;\n" +
                    "    }\n" +
                    "\n" +
                    "    public BusinessException(Throwable e) {\n" +
                    "        super(e);\n" +
                    "    }\n" +
                    "\n" +
                    "    public Integer getCode() {\n" +
                    "        return code;\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public String getMessage() {\n" +
                    "        return message;\n" +
                    "    }\n" +
                    "\n" +
                    "    /**\n" +
                    "     * 重写fillInStackTrace 业务异常不需要堆栈信息，提高效率.\n" +
                    "     */\n" +
                    "    @Override\n" +
                    "    public Throwable fillInStackTrace() {\n" +
                    "        return this;\n" +
                    "    }\n" +
                    "\n" +
                    "    @Override\n" +
                    "    public String toString() {\n" +
                    "        return \"BusinessException{\" +\n" +
                    "                \", code=\" + code +\n" +
                    "                \", message='\" + message + '\\'' +\n" +
                    "                '}';\n" +
                    "    }\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
